package com.chexiehui.jc.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.blankj.utilcode.util.SizeUtils;
import com.chexiehui.jc.Constants;
import com.chexiehui.jc.R;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BaseMvpLazyLoadFragment;
import com.chexiehui.jc.entity.ForumInfo;
import com.chexiehui.jc.mvp.forum.ForumTribuneHotCovenant;
import com.chexiehui.jc.mvp.forum.ForumTribuneHotPresenter;
import com.chexiehui.jc.ui.activity.ForumTribuneDetailsActivity;
import com.chexiehui.jc.utils.BundleBuilder;
import com.chexiehui.jc.utils.GlideUtil;
import com.chexiehui.jc.utils.NoDoubleClickUtils;
import com.chexiehui.jc.widget.LoadingLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Author: SongJun.
 * CreateDate: 2019/1/21 0021.
 * Description: 热门论坛
 */
public class ForumTribuneHotFragment extends BaseMvpLazyLoadFragment<ForumTribuneHotPresenter> implements ForumTribuneHotCovenant.View {

    public static ForumTribuneHotFragment getInstance() {
        ForumTribuneHotFragment fragment = new ForumTribuneHotFragment();
        return fragment;
    }

    @BindView(R.id.recycler)
    RecyclerView mRecycler;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.loadingLayout)
    LoadingLayout loadingLayout;

    private CommonAdapter<ForumInfo> listAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_forum_tribune_hot;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        loadingLayout.setStatus(LoadingLayout.Loading);
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setEnableOverScrollDrag(false);
        refreshLayout.setDisableContentWhenLoading(true);
        refreshLayout.setDisableContentWhenRefresh(true);
        refreshLayout.setEnableFooterFollowWhenLoadFinished(false);
        refreshLayout.setEnableLoadMoreWhenContentNotFull(false);
        refreshLayout.setEnableScrollContentWhenRefreshed(true);
        refreshLayout.setEnableOverScrollDrag(true);

        loadingLayout.setOnReloadListener(v -> {
            loadingLayout.setStatus(LoadingLayout.Loading);
            mvpPresenter.getList();
        });

        refreshLayout.setOnRefreshListener(refreshLayout -> mvpPresenter.getList());

        mRecycler.setLayoutManager(new LinearLayoutManager(mActivity));
        listAdapter = new CommonAdapter<ForumInfo>(mActivity, R.layout.item_forum_tribune_collection, new ArrayList<>()) {
            @Override
            protected void convert(ViewHolder holder, ForumInfo item, int position) {
                holder.setText(R.id.tv_item_name, item.getName());
                GlideUtil.loadImageViewCustomerRoundCrop(mContext, item.getIcon(), holder.getView(R.id.iv_item_image),
                        R.drawable.shape_round_5_gray, R.drawable.shape_round_5_gray,
                        true, true, true, true, SizeUtils.dp2px(5));
            }
        };
        listAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if(NoDoubleClickUtils.isDoubleClick()) return;
                startActivity(ForumTribuneDetailsActivity.class, new BundleBuilder()
                        .putString("forumId", listAdapter.getDatas().get(position).getId())
                        .create());
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        mRecycler.setAdapter(listAdapter);
        isPrepared = true;
        lazyLoad();
    }

    @Override
    protected void lazyLoad() {
        if (!isVisible || !isPrepared || mHasLoadedOnce) {
            return;
        }
        loadingLayout.setStatus(LoadingLayout.Loading);
        mvpPresenter.getList();
    }

    @Override
    protected ForumTribuneHotPresenter createPresenter() {
        return new ForumTribuneHotPresenter(this);
    }

    @Override
    public void onGetListSuccess(BaseModel<List<ForumInfo>> successResult) {
        mHasLoadedOnce = true;
        if (loadingLayout.getStatus() != LoadingLayout.Success) {
            loadingLayout.setStatus(LoadingLayout.Success);
        }
        if (refreshLayout.getState() == RefreshState.Refreshing) {
            refreshLayout.finishRefresh();

        }
        listAdapter.getDatas().clear();
        listAdapter.getDatas().addAll(successResult.getData());
        listAdapter.notifyDataSetChanged();
    }

    @Override
    public void onGetListFailure(BaseModel<Object> failureResult) {
        if (Constants.PAGE_DATA_EMPTY == failureResult.getResultCode()) {
            mHasLoadedOnce = true;
            if (refreshLayout.getState() == RefreshState.Refreshing) {
                refreshLayout.finishRefresh(true);
                if (listAdapter.getDatas().size() > 0) {
                    listAdapter.getDatas().clear();
                    listAdapter.notifyDataSetChanged();
                }
            }
            loadingLayout.setStatus(LoadingLayout.Empty);
            loadingLayout.setEmptyText(failureResult.getResultInfo());
        } else {
            if (refreshLayout.getState() == RefreshState.Refreshing) {
                refreshLayout.finishRefresh(false);
            }
            if (listAdapter.getDatas().size() <= 0) {
                loadingLayout.setStatus(LoadingLayout.Error);
                loadingLayout.setErrorText(failureResult.getResultInfo());
            } else {
                showToast(failureResult.getResultInfo());
            }
        }
    }

}
